package nc.ui.lmqth.iface;

import nc.vo.lmqth.iface.MiddleBVO;
import nc.vo.lmqth.iface.MiddleHVO;
import nc.vo.trade.pub.HYBillVO;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class GenDocumentNode {

	public static Node getVoucherNode(Document document, HYBillVO billvo,String pk_head) {
        if(billvo == null || billvo.getChildrenVO().length == 0){
        	return null;
        }
        
        MiddleHVO headvo = (MiddleHVO)billvo.getParentVO();
        MiddleBVO[] bodyvo = (MiddleBVO[])billvo.getChildrenVO();
        
        Element voucher = document.createElement("po_order");
        voucher.setAttribute("id", pk_head);
        Node voucherhead = getVoucherHeadNode(document, headvo);
        Node voucherbody = getVoucherBodyNode(document, bodyvo);

        voucher.appendChild(voucherhead);
        voucher.appendChild(voucherbody);
        return voucher;
    }
	
	public static Node getVoucherHeadNode(Document document,MiddleHVO hvo){
		Text text = null;
		Element voucherhead = document.createElement("po_order_head");
		//
		String[] names = MiddleHVO.str;
		if(names != null && names.length > 0){
			for(String name : names){
				Element voucher_id = document.createElement(name);
				text = document.createTextNode(name);
				text.setData(hvo.getAttributeValue(name) == null ? "" : hvo.getAttributeValue(name).toString());
				voucher_id.appendChild(text);
				voucherhead.appendChild(voucher_id);
			}
		}
		return voucherhead;
	}

	
	public static Node getVoucherBodyNode(Document document,MiddleBVO[] bodyvo){
		Text text = null;
		Element po_order_body = document.createElement("po_order_body");
		//
		if(bodyvo != null && bodyvo.length > 0){
			for(MiddleBVO bvo : bodyvo){
				Element entry = document.createElement("entry");
				String[] names = MiddleBVO.str;
				if(names != null && names.length > 0){
					for(String name : names){
						Element elment = document.createElement(name);
						text = document.createTextNode(name);
						text.setData(bvo.getAttributeValue(name) == null ? "" : bvo.getAttributeValue(name).toString());
						elment.appendChild(text);
						entry.appendChild(elment);
					}
				}
				po_order_body.appendChild(entry); 
			}
		}
		return po_order_body;
	}
}
